package com.oamicnet.servicetwo.server.service.impl;

import com.oamicnet.servicetwo.server.entity.ProductInfo;
import com.oamicnet.servicetwo.server.mapper.ProductInfoMapper;
import com.oamicnet.servicetwo.server.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductInfoServiceImpl  implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStock(Long id, Integer stock) {
        if (stock < 0) {
            throw new IllegalArgumentException("库存不能小于0");
        }
        ProductInfo productInfo = productInfoMapper.selectById(id);
        productInfo.setStock(productInfo.getStock() - stock);
        return productInfoMapper.updateById(productInfo);
    }
}
